/**
 * Copyright (c) Tapas Mobile.  All Rights Reserved.
 *
 * @author DaiHui 
 * @version 1.0
 */

package com.dotools.utils;

import java.io.File;

import android.content.Context;
import android.os.Environment;


public class PathManager {
    private final static File EXTERNAL_ROOT;
    private final static File DOTOOLS_ROOT;
    private static File THIS_APP_ROOT = null;

    static {
        File f = Environment.getExternalStorageDirectory();

        String MULTI_USER_PREFIX = System.getenv("EMULATED_STORAGE_TARGET");
        if (MULTI_USER_PREFIX == null || MULTI_USER_PREFIX.length() == 0) {
            MULTI_USER_PREFIX = "/storage/emulated";
        }

        if (f.getPath().startsWith(MULTI_USER_PREFIX)) {
            f = new File(System.getenv("EXTERNAL_STORAGE")); // fall back to legacy env
        }

        EXTERNAL_ROOT = f;
        DOTOOLS_ROOT = new File(EXTERNAL_ROOT, "dotools");
    }

    private static File getDirImpl(String path, String usage, boolean internal, boolean seperateForProcess) {
        if (THIS_APP_ROOT == null) {
            THIS_APP_ROOT = new File(DOTOOLS_ROOT, Utilities.getApplicationName());
        }

        // LOG.logI("EXTERNAL_ROOT=" + EXTERNAL_ROOT.getCanonicalPath());

        File file = new File(THIS_APP_ROOT, path);

        if (usage != null) {
            usage = usage.trim();
            if (usage.length() > 0) {
                file = new File(file, usage);
            }
        }

        if (seperateForProcess) {
            file = new File(file, ProcessUtils.getProcessTag());
        }

        FileUtils.ensureDirectory(file.getPath());
        return file;
    }

    public static File getCacheDir(String usage) {
        File file = new File(Utilities.getApplicationContext().getCacheDir(), usage);
        file = new File(file, ProcessUtils.getProcessTag());
        FileUtils.ensureDirectory(file.getPath());
        return file;
    }

    public static File getExternalCacheDir(String usage) {
        File file = new File(Utilities.getApplicationContext().getExternalCacheDir(), usage);
        file = new File(file, ProcessUtils.getProcessTag());
        FileUtils.ensureDirectory(file.getPath());
        return file;
    }

    public static File getLogDir() {
        // always external
        return getDirImpl(".log", null, false, false); // hidden dir
    }

    public static File getDownloadDir() {
        // always external
        return getDirImpl("download", null, false, false);
    }

    public static File getWallpaperDir() {
        // always external
        return getDirImpl("wallpaper", null, false, false);
    }

    public static File getExternalFilesDir() {
        return getDirImpl("file", null, false, false);
    }

    public static File getFilesDir() {
        return Utilities.getApplicationContext().getFilesDir();
    }

    public static File getExternalPandoraDir() {
        return getDirImpl("pandora", null, false, false);
    }

    public static File getPandoraDir() {
        return Utilities.getApplicationContext().getDir("pandora", Context.MODE_PRIVATE);
    }
}
